package com.shareinfo.adminManage.auto.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.WebUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;

import com.shareinfo.base.controller.plug.EasyUIPlug;
import com.shareinfo.base.model.plug.EasyUIModel;
import com.shareinfo.base.pagination.PageInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import com.shareinfo.adminManage.auto.model.AutoDbClassModel;
import com.shareinfo.adminManage.auto.query.AutoDbClassQuery;
import com.shareinfo.adminManage.auto.query.AutoDbTableQuery;
import com.shareinfo.adminManage.auto.service.AutoDbClassService;
import com.shareinfo.adminManage.auto.service.AutoDbTableService;

@Controller
@Scope("prototype")
@RequestMapping(value = "/adminManage/auto/autoDbClass")
public class AutoDbClassController {
	
	@Resource(name = "autoDbClassService")
	private AutoDbClassService autoDbClassService;

	@Resource(name = "autoDbTableService")
	private AutoDbTableService autoDbTableService;
  
	@ModelAttribute
	public AutoDbClassModel get(@RequestParam(required = false) java.lang.String id) {
		if (id != null) {
			return autoDbClassService.getById(id);
		} else {
			return new AutoDbClassModel();
		}
	}
  
  	/** 
	 * 列出对象
	**/
	@RequestMapping(value = {"listForAjax"})
	@ResponseBody
	public Map<String, Object> listForAjax(AutoDbClassQuery autoDbClassQuery,HttpServletRequest request,HttpServletResponse response) {
		PageInfo<AutoDbClassModel> pageInfo = autoDbClassService.selectList(autoDbClassQuery);
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<AutoDbClassModel> list=pageInfo.getList();
		HashMap<String,Long> mapGroupDbClassId=new HashMap<String,Long>();
		if(autoDbClassQuery!=null&&autoDbClassQuery.getIfTreeList()!=0){
			//查询出来分类的个数，用于显示
			AutoDbTableQuery autoDbTableQuery=new AutoDbTableQuery();
			autoDbTableQuery.setDbId(autoDbClassQuery.getDbId());
			List<HashMap> listGroupDbClassId = autoDbTableService.selectGroupDbClassId(autoDbTableQuery);
			//循环listGroupDbClassId，存放到HashMap中，便于后面循环获取并赋值
			for (int i = 0;listGroupDbClassId !=null && i < listGroupDbClassId.size(); i++) {
				mapGroupDbClassId.put((String)listGroupDbClassId.get(i).get("dbClassId"), (Long)listGroupDbClassId.get(i).get("count"));
			}
			AutoDbClassModel autoDbClassModel=null;
			for (int i = 0; i < list.size(); i++) {
				autoDbClassModel=list.get(i);
				if(mapGroupDbClassId.get(autoDbClassModel.getId())!=null)
					autoDbClassModel.setSelfNum(mapGroupDbClassId.get(autoDbClassModel.getId()));
			}
		}
		
		//整理list为easyui的树形结构
		List<AutoDbClassModel> rowsList=EasyUIPlug.treeListBuilder("0", pageInfo.getList());
		if(autoDbClassQuery!=null&&autoDbClassQuery.getIfTreeList()!=0){
			//增加一个未分类
			AutoDbClassModel autoDbClassModel=new AutoDbClassModel();
			autoDbClassModel.setChildren(null);
			autoDbClassModel.setId("0");
			autoDbClassModel.setClassName("未分类");
			autoDbClassModel.setPid("0");
			if(mapGroupDbClassId.get(autoDbClassModel.getId())!=null)
				autoDbClassModel.setSelfNum(mapGroupDbClassId.get(autoDbClassModel.getId()));
			rowsList.add(0, autoDbClassModel);
		}
		map.put(PageInfo.EASYUI_ROWS, rowsList);
		map.put(PageInfo.EASYUI_TOTAL, pageInfo.getTotalRecord()+1);
		return map;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = {"show"})
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		AutoDbClassModel autoDbClass = (AutoDbClassModel)autoDbClassService.getById(id);
		return new ModelAndView("autoDbClassForm","autoDbClass",autoDbClass);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = {"create"})
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,AutoDbClassModel autoDbClassModel) throws Exception {
		return new ModelAndView("autoDbClassForm","autoDbClass",autoDbClassModel);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = {"save"})
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,AutoDbClassModel autoDbClassModel) throws Exception {
		autoDbClassService.save(autoDbClassModel);
		return new ModelAndView("autoDbClassList");
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = {"edit"})
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		AutoDbClassModel autoDbClassModel = (AutoDbClassModel)autoDbClassService.getById(id);
		return new ModelAndView("autoDbClassForm","autoDbClass",autoDbClassModel);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = {"update"})
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		
		AutoDbClassModel autoDbClassModel = (AutoDbClassModel)autoDbClassService.getById(id);
		Map params = WebUtils.getParametersStartingWith(request, "");
		BeanUtilsBean beanUtils = new BeanUtilsBean();
		beanUtils.copyProperties(autoDbClassModel, params);
		autoDbClassService.update(autoDbClassModel);
		return new ModelAndView("autoDbClassList");
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = {"delete"})
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		for(int i = 0; i < ids.length; i++) {
			autoDbClassService.deleteById(ids[i]);
		}
		return new ModelAndView("autoDbClassList");
	}

}

